﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CMS.Core.DataInterfaces;
using CMS.Data;
using CMS.Core.Domain;
using CMS.Presenters.Presenters;
using CMS.Presenters.Views;

namespace CMS.Web.CompDenu
{
    public partial class ListDonThu : BaseUserControl, IDonThuListView
    {
        DonThuListPresenter presenter;
        IList<DonThu> objIList;
        bool isRole = false;
        bool isEdit = true;
        NguoiSuDungInfo userInfo;
        public int SoDTDaXL = 0;
        public int SoDTChuaXL = 0;

        public ListDonThu()
        {
            this.presenter = new DonThuListPresenter(this, NHibernateDaoFactory.Instance.GetDonThuDao());
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            presenter.CancelUpdateEvent += new EventHandler(HandleCancelUpdateEvent);
        }

        private void HandleCancelUpdateEvent(object sender, EventArgs e)
        {
            Response.Redirect(BaseUrl + "&action=VIEW");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //check session
            if (Session["DiaBan"] != null)
            {
                DiaBan obj = NHibernateDaoFactory.Instance.GetDiaBanDao().GetById(Convert.ToInt32(Session["DiaBan"].ToString()));
                if (obj != null)
                {
                    drpDiaBan.Text = obj.TenDiaBan;
                    hidDiaBan.Value = obj.ID.ToString();
                }
            }
            if (Session["NguoiDeDon"] != null)
            {
                txtNguoiDeDon.Text = Session["NguoiDeDon"].ToString();
            }
            if (Session["NoiDungDon"] != null)
            {
                txtNoiDungDon.Text = Session["NoiDungDon"].ToString();
            }
            if (Session["NgayLapDon"] != null)
            {
                txtNgayLapDon.Text = Session["NgayLapDon"].ToString();
            }

            userInfo = (NguoiSuDungInfo)Session["UserInfo"];
            presenter.LoadViewFromModel(userInfo.ID);
            if (!this.IsPostBack)
            {
                this.gridObj.DataSource = this.objIList;
                this.gridObj.DataBind();
            }
            this.lbnXoa.Attributes.Add("onclick", "return doSetStatus(2)");
        }

        // For Ten loai don
        public string getTenLoaiDon(Int32 code)
        {
            try
            {
                return presenter.getLoaiDon(code).TenLoaiDon;
            }
            catch
            {
                return "";
            }
        }

        // For Ten loai don 
        public string getTenLinhVuc(Int32 code)
        {
            try
            {
                return presenter.getLinhVuc(code).TenLinhVuc;
            }
            catch
            {
                return "";
            }
        }

        public string GetNameStartProcess(int status)
        {
            return TrangThaiXuLyDao.Instance.GetById(status).TenTTXL;
        }

        // For Nguoi su dung
        //public string getTenNguoiSuDung(int code)
        //{
        //    try
        //    {
        //        return presenter.getNguoiSuDung(code).TenNguoiSuDung;
        //    }
        //    catch
        //    {
        //        return "";
        //    }
        //}

        #region Grid Members
        // For swich page
        protected void gridObj_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gridObj.DataSource = this.objIList;
            gridObj.PageIndex = e.NewPageIndex;
            gridObj.DataBind();

        }
        #endregion

        #region IDonThuView Members

        public bool IsValid
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public IList<DonThu> DonThuList
        {
            get { return this.objIList; }
            set {
                this.objIList = value;
                foreach (DonThu item in value)
                {
                    //if (item.MaTTXL == 3)
                    //{
                    //    SoDTDaXL++;
                    //}
                    //else
                    //{
                    //    SoDTChuaXL++;
                    //}
                }
            }

        }
        #endregion

        #region Button members
        // For Delete button
        protected void lbnXoa_Click(object sender, EventArgs e)
        {
            string code = "";
            foreach (GridViewRow row in gridObj.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("chkSelect");
                if (cb.Checked)
                {
                    //code = "'" + Convert.ToString(gridObj.DataKeys[row.RowIndex].Value) + "',";
                    code = Convert.ToString(gridObj.DataKeys[row.RowIndex].Value);
                    presenter.ObjToDelete(code);

                }
            }
            presenter.LoadViewFromModel(userInfo.ID);
            this.gridObj.DataSource = this.DonThuList;
            gridObj.DataBind();
        }

        // For Add buuton
        protected void lbnThem_Click(object sender, EventArgs e)
        {
            Response.Redirect(BaseUrl + "&action=ADD");

        }

        // For Update Status button
        protected void lbnHienThi_Click(object sender, EventArgs e)
        {
            //foreach (GridViewRow row in gridObj.Rows)
            //{
            //    CheckBox cb = (CheckBox)row.FindControl("chkSelect");

            //    if (cb.Checked)
            //    {
            //        string code = Convert.ToString(gridObj.DataKeys[row.RowIndex].Value);
            //        presenter.ObjToUpdate(code);

            //    }
            //}
            //presenter.LoadViewFromModel(userInfo.ID);
            //this.gridObj.DataSource = this.DonThuList;
            //gridObj.DataBind();
        }
        #endregion

        protected void gridObj_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Pager)
            {
                //// Disabled control 
                //foreach (GridViewRow row in gridObj.Rows)
                //{
                //    DonThu obj = DonThuDao.Instance.GetById(gridObj.DataKeys[row.RowIndex].Value.ToString());
                //    if (obj.TrangThai == 3 || obj.TrangThai == 0)
                //    {
                //        CheckBox cb = (CheckBox)row.FindControl("chkSelect");
                //        cb.Enabled = false;
                //    }
                //}

                //foreach (Control c in e.Row.Cells[0].Controls)
                //{
                //    if (c.GetType().ToString().Equals("System.Web.UI.WebControls.PagerTable"))
                //    {
                //        Table tbl = (Table)c;
                //        TableCell cell = new TableCell();
                //        cell.Text = "Trang ";
                //        cell.HorizontalAlign = HorizontalAlign.Right;
                //        tbl.Rows[0].Cells.AddAt(0, cell);
                //    }
                //}
            }
        }

        #region IPhongBanView Members


        public bool IsRole
        {
            get
            {
                return this.isRole;
            }
            set
            {
                this.isRole = value;
                //this.lbnThem.Visible = value;
                this.lbnXoa.Visible = value;
            }
        }

        public bool IsEdit
        {
            get
            {
                return this.isEdit;
            }
            set
            {
                this.isEdit = value;
            }
        }
        #endregion

        protected void lbnFilter_Click(object sender, EventArgs e)
        {
            Session["DiaBan"] = hidDiaBan.Value;
            Session["NguoiDeDon"] = txtNguoiDeDon.Text;
            Session["NoiDungDon"] = txtNoiDungDon.Text;
            Session["NgayLapDon"] = txtNgayLapDon.Text.Trim();
            Response.Redirect(BaseUrl);
        }

        protected void lbnDefault_Click(object sender, EventArgs e)
        {
            Session["DiaBan"] = null;
            Session["NguoiDeDon"] = null;
            Session["NoiDungDon"] = null;
            Session["NgayLapDon"] = null;
            Response.Redirect(BaseUrl);
        }

        #region IDonThuListView Members


        public int F_MaDiaBan
        {
            get
            {
                return Convert.ToInt32(hidDiaBan.Value);
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }

        public string F_NguoiDeDon
        {
            get
            {
                return txtNguoiDeDon.Text.Trim();
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }

        public string F_NoiDungDon
        {
            get
            {
                return txtNoiDungDon.Text.Trim();
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }

        public string F_NgayLapDon
        {
            get
            {
                return txtNgayLapDon.Text.Trim();
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }

        #endregion
    }
}